package org.example.chapter3.config;

import org.apache.commons.dbcp2.BasicDataSourceFactory;
import org.example.chapter3.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * @author xshuai
 */
@Configuration
@ComponentScan(value = "org.example.*")
@ImportResource(value = {"classpath:spring-other.xml"})
public class AppConfig {
    @Bean(name = "dataSource")
//    @Conditional(DatabaseConditional.class)
    public DataSource getDataSource() {
        Properties props = new Properties();
        props.setProperty("driver", "com.mysql.Driver");
        props.setProperty("url", "jdbc:mysql://localhost:3306/mysql");
        props.setProperty("username", "root");
        props.setProperty("password", "root");
        DataSource dataSource = null;
        try {
            dataSource = BasicDataSourceFactory.createDataSource(props);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dataSource;
    }

    @Bean(name = "user")
    public User user() {
        User user = new User();
        user.setUserName("张三");
        user.setNote("note");
        user.setId(25L);
        return user;
    }

}

